package app.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

	final static DateFormat formatterBR = new SimpleDateFormat("dd/MM/yyyy");
	final static DateFormat formatterUS = new SimpleDateFormat("yyyy-MM-dd");
	final static DateFormat formatterYear = new SimpleDateFormat("yyyy");
	
	
	public static String getDataAtual() {
		StringBuilder dataAtual = new StringBuilder();
		Calendar calendar = new GregorianCalendar();

		dataAtual.append(calendar.get(Calendar.YEAR));
		dataAtual.append("-");
		dataAtual.append(calendar.get(Calendar.MONTH) + 1);
		dataAtual.append("-");
		dataAtual.append(calendar.get(Calendar.DATE));
		
		return dataAtual.toString();
	}
	
	public static Integer getDiaAtual() {
		Calendar calendar = new GregorianCalendar();
		return calendar.get(Calendar.DATE);
	}
	
	public static Integer getMesAtual() {
		Calendar calendar = new GregorianCalendar();
		Integer mesAtual = calendar.get(Calendar.MONTH);
		
		return mesAtual + 1;
	}
	
	public static Integer getAnoAtual() {
		Calendar calendar = new GregorianCalendar();
		return calendar.get(Calendar.YEAR);
	}
	
	public static String getHoraCompletaAtual() {
		StringBuilder horaAtual = new StringBuilder();
		Calendar calendar = new GregorianCalendar();
		
		horaAtual.append(calendar.get(Calendar.HOUR_OF_DAY));
		horaAtual.append(":");
		horaAtual.append(calendar.get(Calendar.MINUTE));
		horaAtual.append(":");
		horaAtual.append(calendar.get(Calendar.SECOND));
		
		return horaAtual.toString();
	}
	
	public static Integer getHoraAtual() {
		Calendar calendar = new GregorianCalendar();
		
		return calendar.get(Calendar.HOUR_OF_DAY);
	}
	
	public static Integer getHora(String horaCompleta) {
		return Integer.valueOf(horaCompleta.substring(0, 2));
	}
	
	public static Integer getMinutoAtual() {
		Calendar calendar = new GregorianCalendar();
		
		return calendar.get(Calendar.MINUTE);
	}
	
	public static Integer getMinuto(String horaCompleta) {
		return Integer.valueOf(horaCompleta.substring(3, 5));
	}
	
	public static String convertDataBRtoUS(String dataBR) throws ParseException {
		Date dataFormattedBR = (Date) formatterBR.parse(dataBR);
		
		String dataFormattedUS = formatterUS.format(dataFormattedBR);
		
		return dataFormattedUS;
	}
	
	public static String convertDataUStoBR(String dataUS) throws ParseException {
		Date dataFormattedUS = (Date) formatterUS.parse(dataUS);
		
		String dataFormattedBR = formatterBR.format(dataFormattedUS);
		
		return dataFormattedBR;
	}
	
	@SuppressWarnings("deprecation")
	public static String addMonth(String dataUS) throws ParseException {
		
		StringBuilder dataAtual = new StringBuilder();
		Calendar calendar = new GregorianCalendar();
		
		calendar.setTime(formatterUS.parse(dataUS));
		
		dataAtual.append(calendar.get(Calendar.YEAR));
		dataAtual.append("-");
		dataAtual.append(calendar.get(Calendar.MONTH));
		dataAtual.append("-");
		dataAtual.append(calendar.get(Calendar.DATE));
		
		return dataAtual.toString();
	}
	
	public static Date convertDataStrToDate(String data) throws ParseException {
		
		if ("".equals(data) || null == data) {
			return null;
		}
		
		return (Date) formatterUS.parse(data);
	}
	
	public static String convertDateToDataStr(Date data) throws ParseException {
		return formatterUS.format(data);
	}
	
	public static Integer getYear(String data) throws ParseException {
		Date dataFormatted = (Date) formatterUS.parse(data);
		
		Integer year = Integer.valueOf(formatterYear.format(dataFormatted));
		
		return year;
	}
	
	public static Integer getWeekDay(Integer day, Integer month, Integer year) {

		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.YEAR, year);
		
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	public static String getWeekDayName(Integer weekDay) {
		
		String weekDayName = "";
		
		switch (weekDay) {
			case 1:
				weekDayName = "Domingo";
				break;
			case 2:
				weekDayName = "Segunda-feira";
				break;
			case 3:
				weekDayName = "Terça-feira";
				break;
			case 4:
				weekDayName = "Quarta-feira";
				break;
			case 5:
				weekDayName = "Quinta-feira";
				break;
			case 6:
				weekDayName = "Sexta-feira";
				break;
			case 7:
				weekDayName = "Sábado";
				break;
			default:
				break;
		}
		
		return weekDayName;
	}
	
	public static String getMonthName(Integer month) {
		
		String monthName = "";
		
		switch (month) {
		case 1:
			monthName = "Janeiro";
			break;
		case 2:
			monthName = "Fevereiro";
			break;
		case 3:
			monthName = "Março";
			break;
		case 4:
			monthName = "Abril";
			break;
		case 5:
			monthName = "Maio";
			break;
		case 6:
			monthName = "Junho";
			break;
		case 7:
			monthName = "Julho";
			break;
		case 8:
			monthName = "Agosto";
			break;
		case 9:
			monthName = "Setembro";
			break;
		case 10:
			monthName = "Outubro";
			break;
		case 11:
			monthName = "Novembro";
			break;
		case 12:
			monthName = "Dezembro";
			break;
		default:
			break;
		}
		
		return monthName;
	}
	
	public static String formatMonth(Integer month) {
		String monthFormatted = String.valueOf(month);
		
		if (String.valueOf(month).length() == 1) {
			monthFormatted = 0 + "" + month;
		}
		
		return monthFormatted;
	}
}
